using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using SuperMarketManagement.DAO.NHibernate;
using SuperMarketManagement.Entity;
using SuperMarketManagement.Web.Helpers;

namespace SuperMarketManagement.Web.Controllers
{
    [Authorize(Roles = "admin")]
    public class EmployeeController : Controller
    {
        //
        // GET: /Employee/

        public ActionResult List()
        {
            DAOFactory factory = new DAOFactory();
            ViewData.Model = factory.EmployeeDAO.GetAll();
            return View();
        }

        public ActionResult Edit(String u)
        {
            DAOFactory factory = new DAOFactory();
            String username = u;
            Employee employee = null;
            if (String.IsNullOrEmpty(u))
            {
                employee = new Employee();
            }
            else
            {
                employee = factory.EmployeeDAO.GetByUsername(username);
            }
            if (Request.RequestType.Equals("POST", StringComparison.OrdinalIgnoreCase))
            {
                if (employee.AutoId == 0)
                {
                    employee.Username = Request["username"];

                }
                employee.Fullname = Request["fullname"];
                employee.Password = Request["password"];
                employee.Sex = Request["sex"].ToEnum<Sex>();
                employee.DateOfBirth = Convert.ToDateTime(Request["dateOfBirth"]);
                employee.Enabled = Convert.ToBoolean(Request["enabled"]);
                if (!Request["password"].Equals(Request["passwordAgain"]))
                {
                    ViewData["msg"] = "Password does not match";
                    ViewData["passwordAgain"] = Request["passwordAgain"];
                }
                else
                {
                    factory.EmployeeDAO.Save(employee);
                    return RedirectToAction("List");
                }
            }
            ViewData.Model = employee;
            return View();
        }

        public ActionResult AssignRole()
        {
            DAOFactory factory = new DAOFactory();
            String username = Convert.ToString(Request["u"]);
            Employee employee = factory.EmployeeDAO.GetByUsername(username);
            if (Request.RequestType.Equals("POST", StringComparison.OrdinalIgnoreCase))
            {
                employee.RemoveAllRoles();
                foreach (Role r in factory.RoleDAO.GetAll())
                {
                    if (Request["role_" + r.AutoId] != null)
                        employee.AddRole(r);
                }
                factory.EmployeeDAO.Save(employee);
                return RedirectToAction("List");
            }

            IList<Role> allRoles = factory.RoleDAO.GetAll();
            ViewData["allRoles"] = allRoles;
            return View(employee);
        }
    }
}
